<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from "@/views/dashboard/mixins/resize";
import {getClassDatas} from "@/api/system/enginfo";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '500px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
  },
  data() {
    return {
      chart: null,
      pieData: null,
      reportDataLegend:[],
      reportDataSeries:[],

    }
  },
  async created() {
    await this.getData();
    await this.initChart();
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    async getData(){
      await getClassDatas().then(res=>{
        console.log(res.reportdata)
        for(var dataCount = 0; dataCount < res.reportdata.length; dataCount++){
          if (res.reportdata[dataCount].value > 0){
            this.reportDataLegend.push(res.reportdata[dataCount].name);
            this.reportDataSeries.push(res.reportdata[dataCount])
          }
        }

      })
    },
    initChart() {
      var option = {
        title : {
          text : '各个主要分类包含案卷数量',
          subtext : '根据现有归档案卷统计',
          x : 'center'
        },
        tooltip : {
          trigger : 'item',
          formatter : "{a} <br/>{b} : {c} ({d}%)"
        },
        legend : {
          orient : 'vertical',
          x : 'left',
          data : this.reportDataLegend
        },
        toolbox : {
          show : true,
          feature : {
            mark : {
              show : true
            },
            dataView : {
              show : true,
              readOnly : false
            },

            restore : {
              show : true
            },
            saveAsImage : {
              show : true
            }
          }
        },
        calculable : true,
        series : [ {
          name : '数量',
          type : 'pie',
          selectedMode : 'single',
          radius : '55%',
          center : [ '50%', '60%' ],
          data : this.reportDataSeries
        } ]
      };
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption(option)
    },
  }
}
</script>
